package com.lyw.springCloud.controller;

import com.lyw.springCloud.entities.Payment;
import com.lyw.springCloud.result.CommonResult;
import com.lyw.springCloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther liyawei
 * @Date 2022/4/17 11:46
 */

@Slf4j
@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    // 为了测试负载均衡使用的
    @Value("${server.port}")
    private String serverPort;

    // 获取服务器中的每个应用的信息
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/discovery")
    public Object discovery() {

        // 第一种获取方法，通过获取 eureka 中的所有服务列表
        List<String> services = discoveryClient.getServices();
        for (String service : services) {

            log.info("********** element: {}", service);
        }

        // 第二种获取服务信息的方式，通过注入到 eureka 服务中的应用名称精准获取
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getUri());
        }

        return discoveryClient;
    }

    /**
     *  这里需要注意的是因为是post请求前端发送的默认是json格式的对象，所以后端需要使用 @RequestBody 注解接收对象
     * @param payment
     * @return
     */
    @PostMapping("/payment/create")
    public CommonResult<Object> create(@RequestBody Payment payment) {

        int save = paymentService.create(payment);

        log.info(">>>>>>>>>>>>>>>>>>> 插入了一条语句 ：{}", payment);

        if (save > 0) {
            return new CommonResult<>(200, "插入成功, serverPort - " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "插入失败", null);
        }
    }


    @GetMapping("/payment/get/{id}")
    public CommonResult<Object> findPaymentById(@PathVariable("id") Long id) {

        Payment payment = paymentService.getById(id);

        log.info(">>>>>>>>>>>>>>>>>>> 查询了一条语句 ：{}", payment);
        System.out.println("123");
        if (payment != null) {
            return new CommonResult<>(200, "查询成功 serverPort - " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "查询失败， id + " + id, null);
        }
    }

    @GetMapping("/payment/lb")
    public String lb() {
        return serverPort;
    }

    @GetMapping("/payment/lib")
    public String lb1(@RequestParam("uname") String uname) {
        return serverPort;
    }

    @GetMapping("/payment/feign/timeout")
    public String timeout() throws InterruptedException {
        Thread.sleep(6000);
        return serverPort;
    }

    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }
}
